package com.caohx.bms.view;

import com.caohx.bms.pojo.User;
import com.caohx.bms.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class UserManage {

    @Autowired
    private IUserService userService;


    @GetMapping("/userManage")
    public String userManage(Model model,
                             @RequestParam(defaultValue = "0") int page,
                             @RequestParam(defaultValue = "8") int size) {
        Page<User> userPage = userService.listUsers(PageRequest.of(page, size));
        model.addAttribute("users", userPage.getContent());
        model.addAttribute("currentPage", page);
        model.addAttribute("totalPages", userPage.getTotalPages());
        model.addAttribute("size", size); // 确保'size'也被传递到模板
        return "userManage";
    }


    @GetMapping("/addUser")
    public String addUserForm() {

        return "addUser"; // 打开addUser.html页面
    }

    /**
     * 显示编辑用户的表单页面
     */
    @GetMapping("/editUser/{id}")
    public String editUserForm(@PathVariable Integer id, Model model) {
        User user = userService.get(id);
        model.addAttribute("user", user);
        return "editUser"; // 假设有一个名为editUser.html的模板文件
    }

    /**
     * 查看用户的表单页面
     */
    @GetMapping("/viewUser/{id}")
    public String viewUser(@PathVariable Integer id, Model model) {

        User user = userService.get(id);
        model.addAttribute("user", user);
        return "viewUser"; // 返回用户详情页面的模板名称
    }


    @GetMapping("/register")
    public String registerUser() {

        return "register"; // 打开addUser.html页面
    }

    @GetMapping("/login")
    public String userLogin() {

        return "login"; // 打开login.html页面
    }
}
